package com.mallcloud.mall.product.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mallcloud.mall.common.core.util.R;
import com.mallcloud.mall.common.excel.annotation.ResponseExcel;
import com.mallcloud.mall.product.api.entity.AttrModel;
import com.mallcloud.mall.product.api.vo.AttrExtendVO;
import com.mallcloud.mall.product.service.AttrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "属性信息管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/product/attr")
public class AttrController {
    private final AttrService attrService;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('product_attr_view')")
    public R<IPage<AttrModel>> page(Page page, AttrModel dto) {
        return R.ok(attrService.page(page, Wrappers.query(dto)));
    }

    @ApiOperation("列表查询")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('product_attr_view')")
    public R<List<AttrModel>> list(AttrModel dto) {
        return R.ok(attrService.list(Wrappers.lambdaQuery(dto)
                .orderByDesc(AttrModel::getCreateTime)
        ));
    }

    @ApiOperation("所有者查询销售属性树")
    @GetMapping("/ownerTree")
    @PreAuthorize("@pms.hasPermission('product_attr_view')")
    public R<Map<String, List<Tree<Long>>>> ownerTree(AttrModel dto) {
        return R.ok(attrService.ownerTree(dto));
    }

    @ApiOperation("通过id查询")
    @GetMapping("/getInfo")
    @PreAuthorize("@pms.hasPermission('product_attr_view')")
    public R<AttrModel> getInfo(AttrModel dto) {
        return R.ok(attrService.getById(dto.getAttrId()));
    }

    @ApiOperation("新增")
    @PostMapping("/save")
    @PreAuthorize("@pms.hasPermission('product_attr_add')")
    public R<Boolean> save(@RequestBody AttrModel dto) {
        return R.ok(attrService.save(dto));
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    @PreAuthorize("@pms.hasPermission('product_attr_edit')")
    public R<Boolean> updateById(@RequestBody AttrModel dto) {
        return R.ok(attrService.updateById(dto));
    }

    @ApiOperation("删除")
    @DeleteMapping("/{attrId}")
    @PreAuthorize("@pms.hasPermission('product_attr_del')")
    public R<Boolean> removeById(@PathVariable Integer attrId) {
        return R.ok(attrService.removeById(attrId));
    }

    @ApiOperation("批量删除属性信息")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('product_attr_del')")
    public R<Boolean> removeByIds(@RequestBody List<Long> attrIds) {
        return R.ok(attrService.removeByIds(attrIds));
    }

    @ApiOperation("导出")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('product_attr_export')")
    public List<AttrModel> export(AttrModel attr) {
        return attrService.list(Wrappers.query(attr));
    }
}
